/**
 * 
 */
define(
		[ 'jquery', 'platform.common', 'formview', 'methods', 'bootbox',
				'bs.table', 'bs.table-CN' ],
		function($, platform, formview, methods, bootbox) {
			return {
				$tree : $("#codeTreeView"),
				$table : $('#codeTableView'),
				init : function() {
					this.loadTree();
					this.loadGrid();
				},
				loadTree : function() {
					var nodes = methods.loadTreeNodes();
					platform.removeEmptyNode(nodes);
					this.$tree.treeview({
						data : nodes,
						highlightSelected : true,
						selectedBackColor : 'RGB(245,245,245)',
						selectedColor : 'RGB(51,51,51)',
						levels : 0
					});
					var $table = this.$table;
					this.$tree.on('nodeSelected', function(event, data) {
						$table.bootstrapTable('refresh', {
							query : {
								p_code_id : data.id
							}
						});
					});
				},
				loadGrid : function() {
					var columns = [ {
						checkbox : true,
						field : 'checkbox',
					}, {
						field : 'code_id',
						title : 'PK',
						visible : false
					}, {
						field : 'code_value',
						title : '码值'
					}, {
						field : 'code_text',
						title : '中文释义'
					}, {
						field : 'p_code_id',
						title : 'Pid',
						visible : false
					} ];
					this.$table.bootstrapTable({
						url : platform.mapUrl('~/rest/pubcode/list'),
						cache : false,
						method : 'post',
						singleSelect : true,
						striped : true,
						showRefresh : true,
						silent : true,
						pagination : true,
						sidePagination : 'server',
						pageNumber : 1,
						pageSize : 10,
						pageList : [ 10, 15, 20, 50 ],
						queryParams : function(params) {
							return {
								pageNumber : params.offset / params.limit + 1,
								pageSize : params.limit,
								p_code_id : ''
							}
						},
						clickToSelect : true,
						uniqueId : 'code_id',
						toolbar : '#toolbar',
						height : 504,
						columns : columns
					});
				},
				refresh : function() {
					this.loadTree();
					this.$table.bootstrapTable('refresh');
				},
				fnAdd : function() {
					var selectedNode = this.$tree.treeview(true).getSelected();
					if (selectedNode == undefined || selectedNode.length == 0) {
						platform.confirm(bootbox, '系统提示', 'small',
								'确认是否新增公共代码分组?', '确认', '取消', function() {
									formview.showNewForm();
								});
					} else {
						formview.showNewForm(selectedNode[0].id);
					}
				},
				fnRemove : function() {
					var selections = this.$table
							.bootstrapTable('getSelections');
					if (selections == null
							|| selections == undefined
							|| (selections instanceof Array && selections.length != 1)) {
						platform.showtip('remove');
						return;
					}
					platform.confirm(
							'<i class="fa fa-warning"></i>&nbsp;&nbsp;删除提示信息',
							'small', '删除存在子节点的数据时，将删除全部该节点下的数据，确认是否删除?', '确认',
							'取消', function() {
								var code_id = selections[0].code_id;
								methods.del(code_id);
							});
				}
			}
		});